/** 
 * Description:用户反馈类
 * Date:2012-6-10
 * Author:Along	
*/
package com.community.shopping.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.community.shopping.domain.Order;
import com.community.shopping.domain.OrderItem;
import com.community.shopping.domain.User;
import com.community.shopping.domain.UserComment;
import com.community.shopping.service.logic.OrderManager;
import com.community.shopping.service.logic.Page;
import com.community.shopping.service.logic.UserCommentManager;
import com.community.shopping.service.logic.UserManager;


@Controller 
public class UserCommentControl {

	@Autowired  
    private UserCommentManager userCommentManager;
	@Autowired  
    private UserManager userManager;
	private SimpleDateFormat formatDate=new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping("/admin/querycomment")
	public  ModelAndView queryallcomment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		ModelAndView mav = new ModelAndView("commentlist.jsp");   
		String status=request.getParameter("status");	
		if(status==null)
	    {status="1";}
		String beginDate=request.getParameter("beginDate");	
		if(beginDate==null)
		{
			beginDate=formatDate.format(new Date());
		}
		beginDate=beginDate+" 00:00:00";	
		String endDate=request.getParameter("endDate");
		if(endDate==null)
		{
			endDate=formatDate.format(new Date());
		}
	    endDate=endDate+" 24:00:00";
		String filter=" createTime > '"+beginDate+"' and createTime < '"+endDate+"' and status ="+status+" ";
	
        List comments=userCommentManager.queryComment(filter);
        mav.addObject("comments", comments);
        return mav;
	}
	
	@RequestMapping("/admin/recomment")
	public  ModelAndView recomment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
 
		String id=request.getParameter("id");	
		String recontent=request.getParameter("recontent");
		recontent=java.net.URLDecoder.decode(recontent, "UTF-8").replace("\"", "'").replace("\\", ""); 
		userCommentManager.updateReComment(id, recontent);
        return queryallcomment(request,response);
	}
	
	@RequestMapping("/admin/closecomment")
	public  ModelAndView closecomment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
 
		String id=request.getParameter("id");	
		userCommentManager.updateComment(id, 0);
        return queryallcomment(request,response);
	}
	
	@RequestMapping("/admin/opencomment")
	public  ModelAndView opencomment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String id=request.getParameter("id");	
		String recontent=request.getParameter("recontent");
		userCommentManager.updateComment(id, 1);
        return queryallcomment(request,response);
	}
	
	@RequestMapping("/admin/delcomment")
	public  ModelAndView delcomment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String id=request.getParameter("id");	
		userCommentManager.deleteComment(id);
        return queryallcomment(request,response);
	}
	

	/**
	 * 查询所有反馈
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/comment/query")
	public void queryComment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String filter=" status=1";
		Page page=new Page(request,5);	
		List<UserComment> comments=userCommentManager.queryComment(filter,page);
		StringBuilder sb=new StringBuilder("{\"comments\":[");
		for(UserComment comment:comments)
		{
			if(sb.length()>20)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+comment.getId()+"\",");
			if(comment.getUserId()==null)
			{
			sb.append("\"name\":\"未注册用户\",");
			}else
			{
				User user=userManager.getUserById(comment.getUserId().toString());
				String hidename="";
				if(user.getId()==1 || user.getId()==2)
				{
					hidename="admin";
				}
				else
				{
					hidename=user.getUserName().substring(0, 3)+"****"+user.getUserName().substring(7);
				}				
				sb.append("\"name\":\""+hidename+"\",");	
			}
			sb.append("\"content\":\""+comment.getContent()+"\",");
			sb.append("\"createtime\":\""+comment.getCreateTime()+"\",");
			sb.append("\"recontent\":\""+comment.getRecontent()+"\",");
			sb.append("\"retime\":\""+comment.getReTime()+"\"");
			sb.append("}");		
		}
		sb.append("],\"pageno\":\""+page.getCurrentPage()+"\",\"totalpage\":\""+page.getTotalPages()
				+"\",\"totalsize\":\""+page.getTotalRowsAmount()+"\"}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 			
	}
	
	
	/**
	 * 查询我的反馈
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/comment/querymy")
	public void queryMyComment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String userId=request.getParameter("userId");
		String filter=" user.id="+userId+" and status=1";
		Page page=new Page(request,5);	
		List<UserComment> comments=userCommentManager.queryComment(filter,page);
		StringBuilder sb=new StringBuilder("{\"comments\":[");
		for(UserComment comment:comments)
		{
			if(sb.length()>20)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+comment.getId()+"\",");
			if(comment.getUserId()==null)
			{
			sb.append("\"name\":\"未注册用户\",");
			}else
			{
				User user=userManager.getUserById(comment.getUserId().toString());
				sb.append("\"name\":\""+user.getUserName()+"\",");	
			}
			sb.append("\"content\":\""+comment.getContent()+"\",");
			sb.append("\"createtime\":\""+comment.getCreateTime()+"\",");
			sb.append("\"recontent\":\""+comment.getRecontent()+"\",");
			sb.append("\"retime\":\""+comment.getReTime()+"\"");
			sb.append("}");		
		}
		sb.append("],\"pageno\":\""+page.getCurrentPage()+"\",\"totalpage\":\""+page.getTotalPages()
				+"\",\"totalsize\":\""+page.getTotalRowsAmount()+"\"}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 		
	}
	
	/**
	 * 发表反馈
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/comment/add")
	public void addComment(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String userId=request.getParameter("userId");
		String content=request.getParameter("content");
		content=java.net.URLDecoder.decode(content, "UTF-8").replace("\"", "'").replace("\\", ""); 
		userCommentManager.addComment(userId, content);
	}


}
